package com.iotechn.unimall.core.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class RomCodeUtil {

    /** 订单类别头 */

    private static final String ORDER_CODE = "1";
    private static final String MEMBER_CODE = "2";
    private static final String MEMBER_SECRET = "3";

    /** 随即编码 */
    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};
    /** 用户id和随机数总长度 */
    private static final int maxLength = 14;

    /**
              * 更具id进行加密+加随机数组成固定长度编码
              */
    private static String toCode(Long id) {
    String idStr = id.toString();
    StringBuilder idsbs = new StringBuilder();
    for (int i = idStr.length() - 1; i >= 0; i--) {
    idsbs.append(r[idStr.charAt(i) - '0']);
    }
    return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }

     /**
      * 生成时间戳
      */

    private static String getDateTime() {
       DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
       return sdf.format(new Date());
    }

    /**
     * 生成时间戳
     */

    private static String getDateTime1() {
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }

    /**
     * 生成固定长度随机码
     * @param n    长度
     */

    private static long getRandom(long n) {
       long min = 1, max = 9;
       for (int i = 1; i < n; i++) {
       min *= 10;
       max *= 10;
       }
       long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
       return rangeLong;
       }
       /**
              * 生成不带类别标头的编码
              * @param userId
              */

    private static synchronized String getCode(Long userId) {
        userId = userId == null ? 10000 : userId;
        return getDateTime() + toCode(userId);
        }


    /**
     * 生成订单单号编码
     * @param userId
     */
    public static String getOrderCode(Long userId) {
        return ORDER_CODE + getCode(userId);
    }

    public static String getMemberCardCode() {
        return MEMBER_CODE + getDateTime1()+getRandom(6);
    }

    public static String getRandCardCode(){
        return "SNCX"+(int)((Math.random()*9+1)*100000);
    }

    /**
     * 会员卡
     * @return
     */
    public static String getRandCardCode_2(){
        String result=""+(int)((Math.random()*9+1)*100000);
        char[] chars = result.toCharArray();
        for (Integer i = 0 ;i < chars.length ; i++){
            Integer a=0;
            if(Character.isDigit(chars[i])){
                a = Integer.parseInt(String.valueOf(chars[i]));
            }
            if(a - 4 == 0){
                a = ((int)a +MoneyUtils.div(Math.random()*4+1,1d,0).intValue());
            }
            chars[i] = Character.forDigit(a,10);
        }
        result=String.valueOf(chars);
        return result;
    }

    /**
     * java生成随机数字和字母组合10位数
     * @param length 生成随机数的长度
     * @return
     */
    public static String getRandomNickname(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
    public static String getMemberCardSecret() {
        return MEMBER_SECRET + getRandom(11);
    }
}